<template>
  <div class="pageBox">
    <head-component
      title="购物车"
      :back="false"
      :rightText="rightText"
      @onRightText="onRightText"
    ></head-component>
    <div class="noData flex-center" v-if="!cartList.length">
      <img class="noDataImg" src="../../image/cart/noData.png" alt="" />
      <div class="tips">亲，您的购物车还没有宝贝哦，快去添加吧~</div>
    </div>
    <div class="cart" v-else>
      <div class="list">
        <div class="listItem flex-row" v-for="item in cartList" :key="item.id">
          <div class="checkBox" @click="onCheckBox(item)">
            <img
              class="chooseImg"
              src="@/image/cart/weixuanzhong_btn.png"
              alt=""
              v-if="!item.status"
            />
            <img
              class="chooseImg"
              src="@/image/cart/xuanzhong.png"
              alt=""
              v-else
            />
          </div>

          <div class="right flex-between">
            <img class="rightImg" :src="item.image" alt="" />
            <div class="rightInfo flex-between">
              <div class="title">小怡室集 衣物留香珠220g</div>
              <div class="skuBox flex-row">
                <div class="sku">规格：L</div>
                <div class="sku">颜色：红色</div>
              </div>
              <div class="priceBox flex-between">
                <div class="price">￥39.9</div>
                <van-stepper v-model="value" button-size="24px" />
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="tips2" v-if="!manage && disabled">
        不同品类商品不能同时结算
      </div>

      <div class="totalBox">
        <div class="totalCon flex-between">
          <div class="totalLeft flex-between">
            <div class="chooseAll flex-row" @click="onChooseAll">
              <img
                class="chooseAllImg"
                src="@/image/cart/weixuanzhong_btn.png"
                alt=""
                v-if="!chooseAllStatus"
              />
              <img
                class="chooseAllImg"
                src="@/image/cart/xuanzhong.png"
                alt=""
                v-else
              />
              <p>全选</p>
            </div>
            <div class="total flex-row" v-if="!manage">
              <div class="totalNum">共计1件，</div>
              <div class="totalTitle">合计：</div>
              <div class="totalPrice">￥39.90</div>
            </div>
          </div>
          <button
            :class="[disabled ? 'totalBtn totalBtnGrey' : 'totalBtn']"
            :disabled="disabled"
            v-if="!manage"
          >
            结算（2）
          </button>
          <button class="totalBtn" v-if="manage">删除</button>
        </div>
      </div>
    </div>
    <foot-component tab="3"></foot-component>
  </div>
</template>

<script type="text/ecmascript-6">
import { defineComponent, ref, watch } from "vue";
import headComponent from "../../components/headComponent.vue";
import footComponent from "../../components/footComponent.vue";

export default defineComponent({
  components: {
    headComponent,
    footComponent,
  },
  setup() {
    const rightText = ref("管理"); // 右上角文本
    const manage = ref(false); // 控制结算删除显示
    const cartList = ref([
      {
        id: 1,
        image: require("../../image/home/banner.png"),
        status: false,
      },
      {
        id: 2,
        image: require("../../image/home/banner.png"),
        status: false,
      },
      {
        id: 3,
        image: require("../../image/home/banner.png"),
        status: false,
      },
    ]); // 购物车列表
    const statusLength = ref(0); // 选中的购物车列表子元素个数

    const value = ref(1); //步进器

    const chooseAllStatus = ref(false); // 是否全选

    const disabled = ref(false);

    function onRightText() {
      console.log("右上角");
      if (!manage.value) {
        rightText.value = "完成";
        manage.value = !manage.value;
      } else {
        rightText.value = "管理";
        manage.value = !manage.value;
      }
    }

    //选择
    function onCheckBox(param) {
      console.log("选择：", param);
      cartList.value.forEach((item) => {
        if (item.id == param.id) {
          item.status = !item.status;
          if (item.status) {
            statusLength.value++;
          } else {
            statusLength.value--;
          }
        }
      });
    }

    watch(statusLength, (newVal) => {
      if (newVal == cartList.value.length) {
        chooseAllStatus.value = true;
      } else {
        chooseAllStatus.value = false;
      }
    });

    // 全选
    function onChooseAll() {
      chooseAllStatus.value = !chooseAllStatus.value;
      if (chooseAllStatus.value) {
        cartList.value.forEach((item) => {
          item.status = true;
          statusLength.value = cartList.value.length;
        });
      } else {
        cartList.value.forEach((item) => {
          item.status = false;
          statusLength.value = 0;
        });
      }
    }

    return {
      rightText,
      manage,
      cartList,
      value,
      chooseAllStatus,
      disabled,
      statusLength,
      onRightText,
      onCheckBox,
      onChooseAll,
    };
  },
});
</script>

<style scoped lang="less">
.pageBox {
  width: 100%;
  min-height: 100vh;
  padding-top: 88px;
  padding-bottom: 136px;
  background-color: #f5f5f5;
  :deep(.rightTitle) {
    color: #999999 !important;
  }
  .noData {
    flex-direction: column;
    padding-top: 100px;
    .noDataImg {
      width: 360px;
      height: 309px;
    }
    .tips {
      font-size: 26px;
      font-weight: 400;
      color: #999999;
      margin-top: 10px;
    }
  }
  .cart {
    .list {
      background-color: #fff;
      padding: 0 30px;
      .listItem {
        padding: 30px 0;
        border-bottom: 1px solid #f5f5f5;
        .checkBox {
          width: 40px;
          height: 40px;
          .chooseImg {
            width: 100%;
            height: 100%;
          }
        }
        .right {
          padding-left: 20px;
          height: 100%;
          width: 100%;
          .rightImg {
            width: 168px;
            height: 168px;
            border-radius: 10px;
          }
          .rightInfo {
            width: 100%;
            padding-left: 20px;
            flex-direction: column;
            align-items: flex-start;
            height: 100%;
            .title {
              font-size: 28px;
              font-weight: 500;
              color: #333333;
            }
            .skuBox {
              flex-wrap: wrap;
              .sku {
                font-size: 26px;
                font-weight: 400;
                color: #999999;
                margin-right: 28px;
              }
            }
            .priceBox {
              width: 100%;
              .price {
                font-size: 36px;
                font-weight: 500;
                color: #fa4b5a;
              }
            }
          }
        }
      }
    }
    .tips2 {
      margin-top: 51px;
      font-size: 26px;
      font-weight: 400;
      color: #e00b12;
      line-height: 37px;
      text-align: center;
    }
    .totalBox {
      width: 100%;
      height: 136px;
      background: #ffffff;
      box-shadow: 0px -1px 0px 0px #dadee6;
      position: fixed;
      left: 0;
      bottom: 88px;
      z-index: 1;
      .totalCon {
        width: 100%;
        height: 88px;
        .totalLeft {
          width: 100%;
          .chooseAll {
            padding-left: 30px;
            .chooseAllImg {
              width: 36px;
              height: 36px;
            }
            p {
              font-size: 26px;
              font-weight: 400;
              color: #999999;
              margin-left: 20px;
              white-space: nowrap;
            }
          }
          .total {
            width: 100%;
            justify-content: flex-end;
            padding-right: 20px;
            .totalNum {
              font-size: 24px;
              font-weight: 400;
              color: #999999;
            }
            .totalTitle {
              font-size: 26px;
              font-weight: 400;
              color: #333333;
            }
            .totalPrice {
              font-size: 30px;
              font-weight: 400;
              color: #e00b12;
            }
          }
        }
        .totalBtn {
          font-size: 28px;
          font-weight: 500;
          color: #ffffff;
          width: 220px;
          height: 88px;
          background: linear-gradient(270deg, #b30b11 0%, #f50811 100%);
          border: none;
        }
        .totalBtnGrey {
          background: #cccccc;
        }
      }
    }
  }
}
</style>